package chatclient.applets.canvas;




import chatclient.applets.note.Note;
import chatclient.applets.canvas.ChatCanvas;
import chatclient.applets.TextView;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

public class TabCanvas extends ChatCanvas
        implements MouseListener {

    private final Object sync;
    private TextView textview;
    private Vector tabs;
    private Vector widths;
    private int maxHeight;
    private int totalWidth;
    private Image[] images;
    private Image[] spaceImages;
    public final int TOPLEFT = 0;
    public final int LEFT = 1;
    public final int BOTTOMLEFT = 2;
    public final int TOP = 3;
    public final int BOTTOM = 4;
    public final int TOPRIGHT = 5;
    public final int RIGHT = 6;
    public final int BOTTOMRIGHT = 7;
    public final int SELECTED = 8;
    public final int TOPSPACE = 0;
    public final int SPACE = 1;
    public final int BOTTOMSPACE = 2;

    public TabCanvas(TextView textview1, String s, Image[] aimage, Image[] aimage1) {
        super(textview1, s);
        this.sync = new Object();
        this.textview = textview1;
        this.images = aimage;
        this.spaceImages = aimage1;
        removeAllTabs();
        addMouseListener(this);
    }

    public void addTab(Note note) {
        note.setMaxWidth(2147483647);
        this.tabs.addElement(note);
        this.widths.addElement(new Integer(0));
        repaint();
    }

    public void removeTab(int i) {
        this.tabs.removeElementAt(i);
        this.widths.removeElementAt(i);
        repaint();
    }

    public void removeAllTabs() {
        this.tabs = new Vector();
        this.widths = new Vector();
        this.maxHeight = 0;
        this.totalWidth = 0;
        repaint();
    }

    public int findTabAtPosition(int i, int j) {
        synchronized (this.sync) {
            for (int k = 0; k < this.widths.size(); k++) {
                i -= ((Integer) this.widths.elementAt(k)).intValue();
                if (i < 0) {
                    int l = k;
                    return l;
                }
            }
        }

        return -1;
    }

    @Override
    public void mouseClicked(MouseEvent mouseevent) {
        int i = findTabAtPosition(mouseevent.getX(), mouseevent.getY());
        if ((i >= 0) && (i < this.tabs.size())) {
            this.textview.selectedTab(i);
        }
    }

    @Override
    public void mouseEntered(MouseEvent mouseevent) {
    }

    @Override
    public void mouseExited(MouseEvent mouseevent) {
    }

    @Override
    public void mousePressed(MouseEvent mouseevent) {
    }

    @Override
    public void mouseReleased(MouseEvent mouseevent) {
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        
        if (!isVisible()) {
            return;
        }

        boolean flag = false;
        synchronized (this.sync) {
            int i = 0;
            int j = 0;
            int k = 0;
            int l = 0;
            int k1 = this.textview.getSelectedTab();
            for (int i1 = 0; i1 < this.tabs.size(); i1++) {
                Note note = (Note) this.tabs.elementAt(i1);
                int i2 = note.getHeight();
                if (i2 > l) {
                    l = i2;
                }
            }
            for (int j1 = 0; j1 < this.tabs.size(); j1++) {
                Note note1 = (Note) this.tabs.elementAt(j1);
                int j2 = note1.getWidth();
                Color color = note1.getBackground(getStyle());
                byte byte0 = (byte) (j1 != k1 ? 0 : 8);
                int l2 = 0;
                Image image1;
                g.drawImage(image1 = this.images[(byte0 + 0)], i, l2, this);
                l2 += Math.max(0, image1.getHeight(this));
                for (int i3 = 0; i3 < l; i3 += Math.max(1, image1.getHeight(this))) {
                    g.drawImage(image1 = this.images[(byte0 + 1)], i, l2 + i3, this);
                }
                l2 += l;
                g.drawImage(image1 = this.images[(byte0 + 2)], i, l2, this);
                l2 += Math.max(0, image1.getHeight(this));
                i += Math.max(0, image1.getWidth(this));
                l2 = 0;
                for (int j3 = 0; j3 < j2; j3 += Math.max(1, image1.getWidth(this))) {
                    g.drawImage(image1 = this.images[(byte0 + 3)], i + j3, l2, this);
                }
                l2 += Math.max(0, image1.getHeight(this));
                if (color != null) {
                    g.setColor(color);
                    g.fillRect(i, l2, j2, l);
                }
                note1.paint(g, i, l2, getStyle(), false);
                l2 += l;
                for (int k3 = 0; k3 < j2; k3 += Math.max(1, image1.getWidth(this))) {
                    g.drawImage(image1 = this.images[(byte0 + 4)], i + k3, l2, this);
                }
                l2 += Math.max(0, image1.getHeight(this));
                if (l2 > k) {
                    k = l2;
                }
                i += j2;
                l2 = 0;
                g.drawImage(image1 = this.images[(byte0 + 5)], i, l2, this);
                l2 += Math.max(0, image1.getHeight(this));
                for (int l3 = 0; l3 < l; l3 += Math.max(1, image1.getHeight(this))) {
                    g.drawImage(image1 = this.images[(byte0 + 6)], i, l2 + l3, this);
                }
                l2 += l;
                g.drawImage(image1 = this.images[(byte0 + 7)], i, l2, this);
                l2 += Math.max(0, image1.getHeight(this));
                i += Math.max(0, image1.getWidth(this));
                this.widths.setElementAt(new Integer(i - j), j1);
                j = i;
            }

            this.totalWidth = i;
            Image image;
            for (; i < size().width; i += Math.max(1, image.getWidth(this))) {
                int l1 = 0;
                g.drawImage(image = this.spaceImages[0], i, l1, this);
                l1 += Math.max(0, image.getHeight(this));
                for (int k2 = 0; k2 < l; k2 += Math.max(1, image.getHeight(this))) {
                    g.drawImage(image = this.spaceImages[1], i, l1 + k2, this);
                }
                l1 += l;
                g.drawImage(image = this.spaceImages[2], i, l1, this);
                l1 += Math.max(0, image.getHeight(this));
            }

            if (k != this.maxHeight) {
                this.maxHeight = k;
                flag = true;
            }
        }
        if (flag) {
            updateSize();
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(Math.max(1, this.totalWidth), Math.max(1, this.maxHeight));
    }
}
